class Gun(object):
    def __init__(self, model):
        self.model = model
        self.count = 0

    def add_bullet(self, count):
        self.count += count

    def shoot(self):
        if self.count <= 0:
            print("没有子弹了...........")
        else:
            self.count -= 1
            print(f"{self.model}开火，剩余子弹数{self.count}")


class Soldier(object):
    def __init__(self, name, gun: Gun = None):
        # gun: Gun 加上冒号便于编写时可以联想，
        # = None 表示第二个参数是缺省参数
        self.name = name
        self.gun = gun

    def fire(self):
        if self.gun is None:
            print(f"{self.name}没有枪........")
        elif self.gun.count == 0:
            self.gun.add_bullet(2)
            print(f"枪没子弹了，上弹")
        else:
            self.gun.shoot()


if __name__ == '__main__':
    ak47 = Gun("ak47")
    m4a1 = Gun("m4a1")
    man = Soldier("许三多")
    man.gun = ak47
    cmd = int(input("请输入指令"))
    while cmd != 0:
        if cmd == 1:
            man.fire()
        cmd = int(input("请输入指令"))
